﻿// HSS.ServiceModel.VirtualServiceFile.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       VirtualServiceFile.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.ServiceModel
{
	#region Using Directives
	using System.Globalization;
	using System.IO;
	using System.Web;
	using System.Web.Hosting;
	#endregion

	#region VirtualServiceFile
	/// <summary>
	/// Represents an virtual .svc file.
	/// </summary>
#if !DEBUG
	[System.Diagnostics.DebuggerStepThroughAttribute()]
#endif
	public class VirtualServiceFile : VirtualFile
	{
		#region Fields
		private string _Service;
		private string _Factory;
		private bool _debug;
		#endregion

		#region Constructor
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="vp">The virtual path</param>
		/// <param name="service">The service</param>
		/// <param name="factory">The factory</param>
		public VirtualServiceFile(string vp, string service, string factory)
			: base(vp)
		{
			_Service = service;
			_Factory = factory;
			_debug = System.Diagnostics.Debugger.IsAttached;
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Opens the virtual file
		/// </summary>
		/// <returns>A stream containing the contents of the Virtual File</returns>
		public override Stream Open()
		{
			MemoryStream ms = new MemoryStream();
			StreamWriter tw = new StreamWriter(ms);

			string svcName = HttpUtility.HtmlEncode(_Service);
			string svcFact = HttpUtility.HtmlEncode(_Factory);
			string svcFile = "<%@ ServiceHost language=c# Debug=\"" + _debug.ToString() + "\" Service=\"{0}\" Factory=\"{1}\" %>";
			svcFile = string.Format(CultureInfo.InvariantCulture, svcFile, svcName, svcFact);

			tw.Write(svcFile);
			tw.Flush();
			ms.Position = 0;
			return ms;
		}
		#endregion
	}
	#endregion
}